package com.zjsru.plan2024.normal;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: cookLee
 * @Date: 2024-01-14
 * 统计出现过一次的公共字符串
 */
public class CountWords {

    /**
     * 主
     * \
     * 输入：words1 = ["leetcode","is","amazing","as","is"], words2 = ["amazing","leetcode","is"]
     * 输出：2
     * 解释：
     * - "leetcode" 在两个数组中都恰好出现一次，计入答案。
     * - "amazing" 在两个数组中都恰好出现一次，计入答案。
     * - "is" 在两个数组中都出现过，但在 words1 中出现了 2 次，不计入答案。
     * - "as" 在 words1 中出现了一次，但是在 words2 中没有出现过，不计入答案。
     * 所以，有 2 个字符串在两个数组中都恰好出现了一次。
     * \
     * 输入：words1 = ["b","bb","bbb"], words2 = ["a","aa","aaa"]
     * 输出：0
     * 解释：没有字符串在两个数组中都恰好出现一次。
     * \
     * @param args args
     */
    public static void main(String[] args) {
        CountWords countWords = new CountWords();
        String[] words1 = new String[]{"leetcode", "is", "amazing", "as", "is"};
        String[] words2 = new String[]{"amazing", "leetcode", "is"};
        System.out.println(countWords.countWords(words1, words2));
    }

    /**
     * 计算字数
     * 模拟
     * @param words1 words1
     * @param words2 words2
     * @return int
     */
    public int countWords(String[] words1, String[] words2) {
        int ans = 0;
        Map<String, Integer> map1 = new HashMap<>();
        for (String word : words1) {
            map1.put(word, map1.getOrDefault(word, 0) + 1);
        }

        Map<String, Integer> map2 = new HashMap<>();
        for (String word : words2) {
            map2.put(word, map2.getOrDefault(word, 0) + 1);
        }

        for (String word : map1.keySet()) {
            if (map1.get(word) == 1 && map2.getOrDefault(word, 0) == 1) {
                ans++;
            }
        }
        return ans;
    }
}
